package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysMenuDao;
import ysq.xyg.sys.model.po.SysMenu;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysMenuVo;
import ysq.xyg.sys.service.SysMenuService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统管理-菜单service实现
 *
 * @author shengQiang yu
 * @date 2019-4-1 10:31:20
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements SysMenuService {

    @Resource
    private SysMenuDao sysMenuDao;

    private SysMenu sysMenu;

    @Autowired
    public void setBaseDao(SysMenuDao sysMenuDao) {
        this.baseDao = sysMenuDao;
    }


    /**
     * 分页查询
     *
     * @param sysMenuVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    public ReturnEntity page(SysMenuVo sysMenuVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SysMenu> list = acceptList(sysMenuVo);
        PageInfo<SysMenu> page=new PageInfo<SysMenu>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param sysMenuVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    public ReturnEntity addOrUpdate(SysMenuVo sysMenuVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysMenuVo.getId())){
            sysMenu=new SysMenu(currentUser);
            /**
             * 获取菜单的级别和code
             */
            if (StrUtil.isEmpty(sysMenuVo.getParentId())) {
                sysMenu.setMenuGrade(BaseConstant.SYS_MENU_GRADE_ONE);
                //第一级
                SysMenu bm = new SysMenu();
                bm.setMenuGrade(sysMenu.getMenuGrade());
                bm.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                String menuCode = sysMenuDao.AcceptMaxMenuCode(bm);
                if(StringUtil.isEmpty(menuCode)){
                    sysMenu.setMenuCode(BaseConstant.MENU_CODE_MIN);
                }else{
                    sysMenu.setMenuCode(Long.parseLong(menuCode)+1+"");
                }
            } else {
                SysMenu parentMeun = sysMenuDao.selectByPrimaryKey(sysMenuVo.getParentId());
                sysMenu.setMenuGrade((Integer.parseInt(parentMeun.getMenuGrade())+1)+"");
                SysMenu bm = new SysMenu();
                bm.setParentId(sysMenuVo.getParentId());
                bm.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                String menuCode = sysMenuDao.AcceptMaxMenuCode(bm);
                if(StringUtil.isEmpty(menuCode)){
                    sysMenu.setMenuCode(parentMeun.getMenuCode()+ BaseConstant.MENU_CODE_MIN);
                }else{
                    sysMenu.setMenuCode(Long.parseLong(menuCode)+1+"");
                }
            }
            isUpate=false;
        }else{
            sysMenu=selectByPrimaryKey(sysMenuVo.getId());
            if(ObjectUtil.isNull(sysMenu)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysMenu.setUpdateTime(new Date());
            sysMenu.setUpdateUser(currentUser.getId());
        }

        sysMenu.setMenuIcon(sysMenuVo.getMenuIcon());
        sysMenu.setMenuName(sysMenuVo.getMenuName());
        sysMenu.setMenuType(sysMenuVo.getMenuType());
        sysMenu.setPermission(sysMenuVo.getPermission());
        sysMenu.setMenuUrl(sysMenuVo.getMenuUrl());
        sysMenu.setParentId(sysMenuVo.getParentId());
        sysMenu.setSort(sysMenuVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysMenu);
        }else{
            num=insert(sysMenu);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysMenu sysMenu=selectByPrimaryKey(id);
        if(sysMenu!=null){
            sysMenu.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            sysMenu.setUpdateTime(new Date());
            sysMenu.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(sysMenu);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取菜单树
     *
     * @param sysMenuVo 菜单Vo对象
     * @return
     * @author shengQiang yu
     * @date 2019-04-08 9:44
     */
    public ReturnEntity dTreeList(SysMenuVo sysMenuVo) {
        List<SysMenu> list = acceptList(sysMenuVo);
        List<Map<String, Object>> dtreeEntityList = new ArrayList<Map<String, Object>>();
        for (SysMenu sysMenu : list) {
            Map<String, Object> dtreeEntity = new HashedMap();
            dtreeEntity.put("id", sysMenu.getId());
            dtreeEntity.put("title", sysMenu.getMenuName());
            dtreeEntity.put("parentId", StrUtil.isEmpty(sysMenu.getParentId()) ? "0" : sysMenu.getParentId());
            dtreeEntity.put("isLast", isLast(list, sysMenu.getId()));
            Map<String, String> m = new HashedMap();
            m.put("type", "0");
            m.put("isChecked", "0");
            List<Map<String, String>> dtreeCheckEntityList = new ArrayList<Map<String, String>>();
            dtreeCheckEntityList.add(m);
            dtreeEntity.put("checkArr", dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        return new ReturnEntity(dtreeEntityList);
    }
    /**
     * 获取集合
     *
     * @param sysMenuVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    public List<SysMenu> acceptList(SysMenuVo sysMenuVo){
        Example example = new Example(SysMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysMenuVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysMenuVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(sysMenuVo.getMenuName())){
            criteria.andLike("menuName","%"+sysMenuVo.getMenuName()+"%");
        }
        example.setOrderByClause(" sort desc");
        return sysMenuDao.selectByExample(example);
    }

    /**
     * 判断当时对象是否有子对象
     *
     * @param list 菜单集合
     * @param id 父类菜单id
     * @return
     * @author shengQiang yu
     * @date 2019-04-08 9:40
     */
    private boolean isLast(List<SysMenu> list, String id) {
        if (list != null && list.size() > 0 && StrUtil.isNotEmpty(id)) {
            for (SysMenu sysMenu : list) {
                if (id.equals(sysMenu.getParentId())) {
                    return false;
                }
            }
        }
        return true;
    }
}
